package ext.acae2.change.form;

import java.util.List;
import java.util.Map;

import wt.change2.WTChangeOrder2;
import wt.change2.WTChangeOrder2MasterIdentity;
import wt.doc.WTDocument;
import wt.fc.Identified;
import wt.fc.IdentityHelper;
import wt.fc.PersistenceServerHelper;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.pom.Transaction;
import wt.session.SessionServerHelper;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;

import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.meta.common.TypeIdentifierHelper;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.windchill.enterprise.change2.forms.processors.CreateChangeNoticeFormProcessor;


import ext.acae2.change.autonumber.ECNNumberGenerator;
import ext.acae2.change.helper.ECNHelper;

/**
 * @desription change ECN number
 * @author cnie
 * @date 2013-5-12 下午8:41:17
 */
public class ExtCreateChangeNoticeFormProcessor extends
		CreateChangeNoticeFormProcessor {

	@Override
	public FormResult doOperation(NmCommandBean nmcommandbean,
			List<ObjectBean> list) throws WTException {
		// TODO Auto-generated method stub
		Map map  = list.get(0).getChildren().get(0).getParameterMap();
		//String[] strArr = (String[]) list.get(0).getChildren().get(0).getTextParameterValues("addRows_changeTask_affectedItems_table");
		return super.doOperation(nmcommandbean, list);
	}

	@Override
	public FormResult setResultNextAction(FormResult formresult,
			NmCommandBean nmcommandbean, List<ObjectBean> list)
			throws WTException {
		// TODO Auto-generated method stub
//		WTChangeOrder2 ecn = (WTChangeOrder2) list.get(0).getObject();
//		String typeIdentifierStr = TypeIdentifierHelper.getType(ecn)
//				.toExternalForm();
//		if (typeIdentifierStr.indexOf(ChangeConstants.ACAE_TYPE_FILE_ECN) > -1
//				|| typeIdentifierStr
//						.indexOf(ChangeConstants.ACAE_TYPE_PARTDRAWING_ECN) > -1) {
//			String ecnNumber = ecn.getNumber();
//			List<WTObject> affectObjs = ECNHelper.getChangeAffectItem(ecn);
//			if(affectObjs!=null&&affectObjs.size()>0){
//				WTObject wtobj = affectObjs.get(0);
//				String objNumber = null;
//				if (wtobj instanceof WTPart) {
//					WTPart part = (WTPart) wtobj;
//					objNumber = part.getNumber();
//				} else if (wtobj instanceof WTDocument) {
//					WTDocument doc = (WTDocument) wtobj;
//					objNumber = doc.getNumber();
//				}
//				if (ecnNumber.indexOf(objNumber
//						+ ChangeConstants.ACAE_ECN_NUMBER_DEFAULT_CODE) < 0) {
//					ecnNumber = ECNNumberGenerator.getECNNumber(wtobj);
//					resetECNNumber(ecn, ecnNumber);
//				}
//			}
//			
//		}
		return super.setResultNextAction(formresult, nmcommandbean, list);
	}
	
	/**
	 * 
	 * @description 
	 *   reset ECN number
	 * @author cnie
	 * @date 2013-5-12 下午10:53:42
	 * @version 1.0
	 * @param co
	 * @param number
	 */
	protected synchronized void resetECNNumber(WTChangeOrder2 co, String number) {
		boolean access = SessionServerHelper.manager.setAccessEnforced(false);

		Transaction transaction = null;
		try {
			transaction = new Transaction();
			transaction.start();

			Identified identified = (Identified) co.getMaster();
			WTChangeOrder2MasterIdentity masteridentity = (WTChangeOrder2MasterIdentity) identified
					.getIdentificationObject();
			masteridentity.setNumber(number);
			identified = IdentityHelper.service.changeIdentity(identified,
					masteridentity); // 重命名
			PersistenceServerHelper.manager.update(co.getMaster());

			transaction.commit();
			transaction = null;
		} catch (WTException e) {
			e.printStackTrace();
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
		} finally {
			if (transaction != null)
				transaction.rollback();

			SessionServerHelper.manager.setAccessEnforced(access);
		}
	}

}
